
package com.cyanite.playlv2;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;

public class ShowImageDialog extends Dialog {

    private Context mContext;
    private Bitmap mBitmap;
    private LinearLayout mLayout;
    private int mWidth;

    public ShowImageDialog(Context context, Bitmap bmp, int width) {
        super(context);
        mContext = context;
        mBitmap = bmp;
        mWidth = width;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLayout = new LinearLayout(mContext);
        mLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        setContentView(mLayout);
        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });

        WindowManager.LayoutParams wLayoutParams = getWindow().getAttributes();
        wLayoutParams.x = 0; // align center
        wLayoutParams.y = 0; // align center

        // we set screen's width as dialog's width's 95%, then calculate the height by keeping the aspect ratio of the original picture.
        wLayoutParams.width = mWidth*95/100;
        wLayoutParams.height = mBitmap.getHeight() * wLayoutParams.width / mBitmap.getWidth();

        getWindow().setAttributes((android.view.WindowManager.LayoutParams) wLayoutParams);
        getWindow().setBackgroundDrawable(new BitmapDrawable(mBitmap));
        setCanceledOnTouchOutside(true);
    }
}
